layui.define(["form", "jquery", "commonServices"], function (exports) {
	var form = layui.form,
		$ = layui.jquery,
		commonServices = layui.commonServices,
		Address = {
			/**
			 * @author: ares
			 * @date: 2020/2/18 16:52
			 * @description: 渲染省|直辖市下拉列表
			 */
			provinces: function () {
				var provinceFrag = $(document.createDocumentFragment()),
					that = this;
				provinceFrag.append('<option value="">请选择省</option>')
				commonServices.getAddressService().then(function (res) {
					var data = res.data;
					for (var i = 0; i < data.length; i++) {
						provinceFrag.append('<option value="' + data[i].code + '">' + data[i].name + '</option>');
					}
					$("select[name=province]").empty().append(provinceFrag);
					form.render();
					form.on('select(province)', function (proData) {
						$("select[name=area]").empty().append('<option value="">请选择区/县</option>');
						var value = 1*proData.value;
						if (value > 0) {
							that.citys(data[$(this).index() - 1].childs);
						} else {
							$("select[name=city]").empty().append('<option value="">请选择市</option>');
							$("select[name=area]").empty().append('<option value="">请选择区/县</option>');
						}
					});
				})
			},
			/**
			 * @author: ares
			 * @date: 2020/2/18 16:52
			 * @description: 渲染城市下拉列表
			 * @param {array}citys
			 */
			citys: function (citys) {
				var cityFrag = $(document.createDocumentFragment()),
					that = this;
				cityFrag.append('<option value="">请选择市</option>')
				for (var i = 0; i < citys.length; i++) {
					cityFrag.append('<option value="' + citys[i].code + '">' + citys[i].name + '</option>');
				}
				$("select[name=city]").empty().append(cityFrag);
				form.render();
				form.on('select(city)', function (cityData) {
					var value = 1*cityData.value;
					if (value > 0) {
						that.areas(citys[$(this).index() - 1].childs);
					} else {
						$("select[name=area]").empty().append('<option value="">请选择区/县</option>');
					}
				});
			},
			/**
			 * @author: ares
			 * @date: 2020/2/18 16:51
			 * @description: 渲染区县下拉列表
			 * @param {array}areas
			 */
			areas: function (areas) {
				var areaFram = $(document.createDocumentFragment());
				areaFram.append('<option value="">请选择县/区</option>')
				for (var i = 0; i < areas.length; i++) {
					areaFram.append('<option value="' + areas[i].code + '">' + areas[i].name + '</option>');
				}
				$("select[name=area]").empty().append(areaFram);
				form.render();
			}
		};
	exports("address", Address);
})